using System;
using System.Collections.Generic;
using System.Linq;
using VoiceAd.Models;
using System.Web.Security;

namespace VoiceAd.DAL
{
    public class CampaignRepository : ICampaignRepository, IDisposable
    {
        private dbVoiceAdDataContext context = new dbVoiceAdDataContext();

        public CampaignRepository(dbVoiceAdDataContext db)
        {
            this.context = db;
        }

        public IEnumerable<tblCampaign> GetListCampaigns()
        {
            return context.tblCampaigns.ToList();
        }

        public IEnumerable<tblCampaign> GetListCampaignsByUserId(string strUserId)
        {
            return context.tblCampaigns.Where(x => x.tblCustomer.UserId == strUserId).ToList();
        }

        public IEnumerable<tblCampaign> GetListCampaignsbyCusId()
        {
            string strUser = Membership.GetUser().UserName;
            return context.tblCampaigns.Where(x => x.tblCustomer.UserName == strUser && x.EndDay < DateTime.Now).ToList();
        }

        public IEnumerable<tblCampaign> GetListCampaignsbyCusId(string strTitle)
        {
            string strUser = Membership.GetUser().UserName;
            strTitle = strTitle.ToLower();
            return context.tblCampaigns.Where(x => x.tblCustomer.UserName == strUser && x.Title.ToLower().Contains(strTitle)).ToList();
        }

        public IEnumerable<tblDistrictProvince> CampaignGetListDistrict(int pCampaignID, int pCusId)
        {
            return context.tblCampaignGetListDistrict(pCampaignID, pCusId);
        }

        public IEnumerable<tblCampaign> GetListActiveCampaignsbyCusId()
        {
            string strUser = Membership.GetUser().UserName;
            return context.tblCampaigns.Where(x => x.tblCustomer.UserName == strUser && x.EndDay >= DateTime.Now).ToList();
        }

        public IEnumerable<tblCampaign> GetListCampaignsbyStationId()
        {
            string strUser = Membership.GetUser().UserName;
            return context.tblCampaignGetListByStation(strUser);
        }

        public IEnumerable<tblCampaign> GetListActiveCampaignsbyStationId()
        {
            string strUser = Membership.GetUser().UserName;
            return context.tblCampaignGetListActiveByStation(strUser);
        }

        public IEnumerable<tblExcel> PrintCampaign(int id)
        {
            return context.tblCampaignPrintExcel(id);
        }

        public int GetCountDay(int campId, int stationId, int monthId)
        {
            string strResult = string.Empty;
            int intCount = 0;
            tblBooking obj = context.tblBookings.Where(x => x.CampaignId == campId && x.StationId == stationId).FirstOrDefault();
            if (obj != null)
            {
                tblBlock tbl = context.tblBlocks.Where(x => x.BookingId == obj.Id && x.MonthBook == monthId).FirstOrDefault();
                if (tbl != null)
                    strResult = tbl.DayBook;
                if (!string.IsNullOrEmpty(strResult))
                    intCount = strResult.Split(',').Count();
            }
            return intCount;
        }
        public int GetSumStation(int id)
        {
            int? intCount = 0;
            context.tblCampaignGetSumStation(id, ref intCount);
            return intCount.Value;
        }

        public tblCampaign GetCampaignById(int id)
        {
            return context.tblCampaigns.Where(x => x.Id == id).FirstOrDefault();
        }

        public IEnumerable<tblPrintSchedule> PrintSchedule(int CampId, int StationId, int monthId)
        {
            return context.tblCampaignPrintSchedule(CampId, StationId, monthId);
        }

        public IEnumerable<tblPrintSchedule> PrintCertificates(int CampId, int StationId, int MonthId)
        {
            return context.tblCampaignPrintCertificates(CampId, StationId, MonthId);
        }

        public IEnumerable<tblListMonth> tblCampaignGetListMonth(int CampId)
        {
            return context.tblCampaignGetListMonth(CampId);
        }

        public void InsStationByCampId(int campId, int stationId)
        {
            context.tblCampaignInsertStation(campId, stationId);
        }

        public void InsCampaign(tblCampaign tblcampaign)
        {
            context.tblCampaigns.InsertOnSubmit(tblcampaign);
            context.SubmitChanges();
        }

        public void UpdCampaign(tblCampaign tblcampaign)
        {
            tblCampaign obj = context.tblCampaigns.Where(x => x.Id == tblcampaign.Id).First();
            obj.CustomerId = tblcampaign.CustomerId;
            obj.Title = tblcampaign.Title;
            obj.BeginDay = tblcampaign.BeginDay;
            obj.EndDay = tblcampaign.EndDay;
            obj.UrlBooking = tblcampaign.UrlBooking;
            obj.Approve = tblcampaign.Approve;
            obj.SoNgay = tblcampaign.SoNgay;
            obj.DonGia = tblcampaign.DonGia;
            obj.ChietKhau = tblcampaign.ChietKhau;
            context.SubmitChanges();
        }

        public void DelCampaign(int id)
        {
            tblCampaign obj = context.tblCampaigns.Single(x => x.Id == id);
            context.tblCampaigns.DeleteOnSubmit(obj);
            context.SubmitChanges();
        }

        public int DeleteCampaignWithDetail(int id)
        {
            return context.DeleteCampaignWithDetail(id);
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
